package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;

@RequestMapping("/admin/report")
@RestController
@Slf4j
@Api(tags = "数据统计相关接口")
public class ReportController {
    @Autowired
    private ReportService reportService;

    @GetMapping("/turnoverStatistics")
    @ApiOperation("营业额统计接口")
    public Result<TurnoverReportVO> turnoverStatistics(//使用TurnoverReportVO封装结果
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,//@DateTimeFormat来确定日期格式
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("{}~{}营业额统计", begin, end);
        TurnoverReportVO turnoverReport = reportService.getTurnoverStatistics(begin,end);
            return Result.success(turnoverReport);
    }

    @GetMapping("/userStatistics")
    @ApiOperation("用户统计接口")
    public Result<UserReportVO> userStatistics(//使用TurnoverReportVO封装结果
        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,//@DateTimeFormat来确定日期格式
        @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("{}~{}用户数量统计", begin, end);
        UserReportVO userReport = reportService.getUserReport(begin,end);
        return Result.success(userReport);
    }


    @GetMapping("/ordersStatistics")
    @ApiOperation("订单统计接口")
    public Result<OrderReportVO> ordersStatistics(//使用TurnoverReportVO封装结果
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,//@DateTimeFormat来确定日期格式
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("{}~{}订单数量统计", begin, end);
        OrderReportVO orderReport = reportService.getOrderPort(begin,end);
        return Result.success(orderReport);
    }

    @GetMapping("/top10")
    @ApiOperation("销量排行接口")
    public Result<SalesTop10ReportVO> top(//使用TurnoverReportVO封装结果
         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,//@DateTimeFormat来确定日期格式
         @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("{}~{}订单销量排行统计", begin, end);
        SalesTop10ReportVO salesTop10Report =  reportService.getTop(begin,end);
        return Result.success(salesTop10Report);
    }
    @GetMapping("/export")
    @ApiOperation("导出excel表格接口")
    public void excel(HttpServletResponse response) throws IOException {
        //进行写入操作
        reportService.getExcel(response);
    }
}
